package com.lmaos.allcontrol.beancontrol.class_;

import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.lmaos.allcontrol.beancontrol.utils_.NumberUtils;

/** 类工具
 * 
 * @author lmaos 
 * 2016年11月14日 下午3:19:28
 */
public class ClassUtils {
	
	
	/** 获取这个class ，如果不存在返回null
	 * 
	 * @param className
	 * @return
	 */
	public static Class getClass(String className){
		Class cl = null;
		try {
			cl = Class.forName(className);
		} catch (ClassNotFoundException e) {
		}
		return cl;
	}
	
	public static Class getClass(String className,ClassLoader classLoader){
		Class cl = null;
		try {
			cl = classLoader.loadClass(className);
		} catch (ClassNotFoundException e) {
		}
		return cl;
	}
	
	/** 获取这个参数当所有对应当类
	 * 
	 * @param params
	 * @return
	 */
	public static Class[] getClasssByParams(Object[] params,boolean base){
		if(params==null){
			return null;
		}
		Class[] cls = new Class[params.length];
		for (int i = 0; i < cls.length; i++) {
			Object param = params[i];
			if(param!=null){
				if(base){
					cls[i] = getBaseType(param.getClass());
				}else{
					cls[i] = param.getClass();
				}
			}
		}
		return cls;
	}
	
	/** 获取原始当基本类型
	 * 
	 * @param cl
	 * @return
	 */
	public static Class getBaseType(Class cl){
		if(cl == Integer.class){
			cl = int.class;
		}else if(cl == Long.class){
			cl = long.class;
		}else if(cl == Float.class){
			cl = float.class;
		}else if(cl == Short.class){
			cl = short.class;
		}else if(cl == Byte.class){
			cl = byte.class;
		}else if(cl == Double.class){
			cl = double.class;
		}else if(cl == Boolean.class){
			cl = boolean.class;
		}else if(cl == Character.class){
			cl = char.class;
		}
		else if(cl == Integer[].class){
			cl = int[].class;
		}else if(cl == Long[].class){
			cl = long[].class;
		}else if(cl == Float[].class){
			cl = float[].class;
		}else if(cl == Short[].class){
			cl = short[].class;
		}else if(cl == Byte[].class){
			cl = byte[].class;
		}else if(cl == Double[].class){
			cl = double[].class;
		}else if(cl == Boolean[].class){
			cl = boolean[].class;
		}else if(cl == Character[].class){
			cl = char[].class;
		}
		
		return cl;
	}
	
	public static Class getBasheType(String type,ClassLoader classLoader){
		if(type.equals("int")){
			return int.class;
		}else if(type.equals("short")){
			return short.class;
		}else if(type.equals("byte")){
			return byte.class;
		}else if(type.equals("long")){
			return long.class;
		}else if(type.equals("float")){
			return float.class;
		}else if(type.equals("double")){
			return double.class;
		}else if(type.equals("boolean")){
			return boolean.class;
		}else if(type.equals("char")){
			return char.class;
		}else if(type.equals("string")){
			return String.class;
		}
		else if(type.equals("int[]")){
			return int[].class;
		}else if(type.equals("short[]")){
			return short[].class;
		}else if(type.equals("byte[]")){
			return byte[].class;
		}else if(type.equals("long[]")){
			return long[].class;
		}else if(type.equals("float[]")){
			return float[].class;
		}else if(type.equals("double[]")){
			return double[].class;
		}else if(type.equals("boolean[]")){
			return boolean[].class;
		}else if(type.equals("char[]")){
			return char[].class;
		}else if(type.equals("string[]")){
			return String[].class;
		}else{
			try {
				return classLoader.loadClass(type);
			} catch (ClassNotFoundException e) {
				return null;
			}
		}
		
		
	}
	
	/** 是否是一个基本的类 比如int Integer等
	 * 
	 * @param cl
	 * @return
	 */
	public static boolean isBaseType(Class cl){
		return getBaseType(cl).isPrimitive();
	}
	public static boolean isBaseArray(Class cl){
		cl = getBaseType(cl);
		if(cl == int[].class){
			return true;
		}else if(cl == long[].class){
			return true;
		}else if(cl == float[].class){
			return true;
		}else if(cl == short[].class){
			return true;
		}else if(cl == byte[].class){
			return true;
		}else if(cl == double[].class){
			return true;
		}else if(cl == boolean[].class){
			return true;
		}else if(cl == char[].class){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 说明：获得value，如果value为null 则通过type获得默认值		<br>
	 * 时间：2017年12月17日 下午6:06:09
	 *
	 * @param value
	 * @param type
	 * @return
	 */
	public static Object getValueAndNullSetDefVal(Object value,Class type){
		if(value == null){
			value = getDefaultValue(type);
		}
		return value;
	}
	/** 获取类型的默认值
	 * 
	 * @param cl
	 * @return
	 */
	public static Object getDefaultValue(Class cl){
		Object value = null;
		if(cl == int.class){
			value = 0;
		}else if(cl == long.class){
			value = 0L;
		}else if(cl == float.class){
			value = 0.0f;
		}else if(cl == short.class){
			value = (short)0;
		}else if(cl == byte.class){
			value = (byte)0;
		}else if(cl == double.class){
			value = 0.0d;
		}else if(cl == boolean.class){
			value = false;
		}else if(cl == char.class){
			value = '\u0000';
		}
		
		return value;
	}
	
	public static Object getDefaultValue(Class cl,String text){
		if(text == null || text.isEmpty()){
			return getDefaultValue(cl);
		}
		Object value = text;
		if(cl == String.class){
			
		}else if(cl == int.class){
			value = NumberUtils.toInt(text, 0);
		}else if(cl == long.class){
			value = NumberUtils.toLong(text, 0);
		}else if(cl == float.class){
			value = NumberUtils.toFloat(text, 0);
		}else if(cl == short.class){
			value = NumberUtils.toShort(text, (short) 0);
		}else if(cl == byte.class){
			value = NumberUtils.toByte(text, (byte) 0);
		}else if(cl == double.class){
			value = NumberUtils.toDouble(text, 0);
		}else if(cl == boolean.class){
			value = NumberUtils.toBoolean(text);
		}else if(cl == char.class){
			value = text.charAt(0);
		}
		
		return value;
	}
	
	/** 获取对象，构造函数当参数如果是基本类型，不可设置为封装类
	 * 
	 * @param cl
	 * @param params
	 * @return
	 */
	public static <T>T getBaseObject(Class cl,Object... params){
		if(cl==null){
			return null;
		}
		T obj = null;
		try {
			Class[] cls = getClasssByParams(params,true);
			Constructor<T> constructor = cl.getDeclaredConstructor(cls);
			if(!constructor.isAccessible()){
				constructor.setAccessible(true);
			}
			obj = constructor.newInstance(params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obj;
	}
	
	/** 获取这个类对象
	 * 
	 * @param cl
	 * @param clParams	参数类型
	 * @param valParams	参数值
	 * @return
	 */
	public static <T>T getObject(Class cl,Class[] clParams,Object[] valParams){
		T obj = null;
		try {
			Constructor<T> constructor = cl.getDeclaredConstructor(clParams);
			if(!constructor.isAccessible()){
				constructor.setAccessible(true);
			}
			obj = constructor.newInstance(valParams);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obj;
	}
	
	public static Constructor indexConstructorByAnnotation(Class cl,Class annotationClass){
		try {
			Constructor[] constructors = cl.getDeclaredConstructors();
			for (Constructor constructor : constructors) {
				if(constructor.isAnnotationPresent(annotationClass)){
					if(!constructor.isAccessible()){
						constructor.setAccessible(true);
					}
					return constructor;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static Constructor indexConstructorByTypes(Class cl,Class[] types){
		try {
			Constructor constructor = cl.getDeclaredConstructor(types);
			return constructor;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/** 获取对象，必须要有无参构造
	 * 
	 * @param cl
	 * @return
	 */
	public static <T>T getObject(Class cl){
		return getBaseObject(cl);
	}
	
	public static <T>T getObject(String className){
		return getBaseObject(getClass(className));
	}
	
	/** 获取当前class 包括自己和父类，接口父接口。
	 * 
	 * @param cl
	 * @param loadInterfacesClasss 当值为true 加载接口,false 不加载接口
	 * @return
	 */
	public static List<Class> getClassAll(Class cl,boolean loadInterfacesClasss){
		List<Class> cls = new ArrayList<Class>();
		cls.add(cl);
		for (int i = 0; i < cls.size(); i++) {
			Class subClass = cls.get(i);
			Class superClass = subClass.getSuperclass();
			if(superClass!=null&&superClass != Object.class){
				cls.add(superClass);
			}
			if(loadInterfacesClasss){
				Class[] interfacesClasss = subClass.getInterfaces();
				if(interfacesClasss!=null&&interfacesClasss.length>0)
				for (Class interfacesClass : interfacesClasss) {
					cls.add(interfacesClass);
				}
			}
		}
		return cls;
	}
	
	/** 是否被 final 修饰, 实际上调用 Modifier.isFinal(member.getModifiers());<br>
	 *  更多方法参考 java.lang.reflect.Modifier
	 * 
	 * @param member
	 * @return
	 */
	public static boolean isFinal(Member member){
		return Modifier.isFinal(member.getModifiers());
	}
	
	/** 是否被 static 修饰, 实际上调用 Modifier.isStatic(member.getModifiers());<br>
	 *  更多方法参考 java.lang.reflect.Modifier
	 * 
	 * @param member
	 * @return
	 */
	public static boolean isStatic(Member member){
		return Modifier.isStatic(member.getModifiers());
	}
	
	public static boolean isStaticOrFinal(Member member){
		return isStatic(member)||isFinal(member);
	}
	
	public static boolean isStaticAndFinal(Member member){
		return isStatic(member)&&isFinal(member);
	}
	
	/** 过滤被 static 或 final 修饰的成员
	 * 
	 * @param members
	 */
	public static void filterStaticOrFinal(List<? extends Member> members){
		Iterator<?extends Member> iterator = members.iterator();
		while(iterator.hasNext()){
			Member member = iterator.next();
			if(ClassUtils.isStaticOrFinal(member)){
				iterator.remove();
			}
		}
	}
	/**
	 * 说明：判断当前类是不是这个父类的		<br>
	 * 时间：2017年12月17日 下午7:12:22
	 *
	 * @param subClass
	 * @param superClass
	 * @return
	 */
	public static boolean isSuper(Class subClass,Class superClass){
		try {
			return superClass.isAssignableFrom(subClass);
		} catch (Exception e) {
			return false;
		}
	}
}
